package 力扣91;

import java.util.Stack;

/**
 * @ClassName : 232用栈实现队列
 * @Author : 骆发茂
 * @Date: 2021/12/19 18:10
 * @Description :
 */
public class Day5_232用栈实现队列 {

    class MyQueue {
        private Stack<Integer> a;//输入栈
        private Stack<Integer> b;//输出栈

        public MyQueue() {
            a = new Stack<>();
            b = new Stack<>();
        }

        public void push(int x) {
            a.push(x);
        }

        //int pop() 从队列的开头移除并返回元素
        public int pop() {
            if (b.isEmpty()) {
                while (!a.isEmpty()) {
                    b.push(a.pop());
                }
            }
            return b.pop();
        }

        //int peek() 返回队列开头的元素
        public int peek() {
            if (b.isEmpty()) {
                while (!a.isEmpty()) {
                    b.push(a.pop());
                }
            }
            return b.peek();
        }

        public boolean empty() {
            return a.isEmpty() && b.isEmpty();
        }
    }
}